<?php


namespace app\api\controller\finance;


use app\api\controller\Api;
use app\common\order\model\OrderIncome;
use util\Format;

class Income extends Api
{
    public function index()
    {
        $post = $this->request->post();

        $where = [];
        // 审核状态
        if (!empty($post['check_status'])) $where[] = ['OrderIncome.check_status', 'in', $post['check_status']];
        // 收款方式
        if (!empty($post['income_way'])) $where[] = ['OrderIncome.income_way_id', 'in', $post['income_way']];
        // 收款性质
        if (!empty($post['income_type'])) $where[] = ['OrderIncome.income_type_id', 'in', $post['income_type']];
        // total检索
        if (!empty($post['total'])) $where[] = ['OrderIncome.total', '=', $post['total']];
        // 收款区间检索
        if (!empty($post['income_range'])) {
            $incomeRange = Format::dateRange($post['income_range']);
            $where[] = ['OrderIncome.income_date', 'between', $incomeRange];
        }


        $hasWhere = [];
        $hasWhere[] = ['Order.refund_status', '=', 0];
        if (!empty($post['user_id'])) $hasWhere[] = ['Order.user_id', 'in', $post['user_id']];
        if (!empty($post['company_id'])) $hasWhere[] = ['Order.company_id', 'in', $post['company_id']];
        if (!empty($post['store_id'])) $hasWhere[] = ['Order.store_id', 'in', $post['store_id']];
        if (!empty($post['category_id'])) $hasWhere[] = ['Order.category_id', 'in', $post['category_id']];
        if (!empty($post['order_type'])) $hasWhere[] = ['Order.order_type', 'in', $post['order_type']];
        // if (!empty($post['check_status'])) $hasWhere[] = ['check_status', 'in', $post['check_status']];
        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $hasWhere[] = ['Order.sign_date', 'between', $signDateRange];
        }
        if (!empty($post['event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $hasWhere[] = ['Order.event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $hasWhere[] = ['Order.contact_no|Order.bridegroom|Order.bridegroom_mobile|Order.bride|Order.bride_mobile', 'like', "%{$post['keyword']}%"];

        list($cfg) = Format::pagination($post);
        $model = OrderIncome::hasWhere('order', $hasWhere);

        $result = [];
        if (empty($post['is_export'])) {
            $pg = $model->with('order')->where($where)->order('id desc')->paginate($cfg);
            $rows = $pg->getCollection();
            foreach ($rows as $row) {
                $row->append(['way', 'type', 'user', 'check_tag']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'hall', 'banquet_type', 'order_type_text', 'check_tag']);
            }
            $result['list'] = $rows;
            $result['pagination'] = Format::pgResult($pg);
            return success($result);
        } else {
            $expData = [];
            // 定义表头
            // 审核状态、签单公司、合作模式、订单类型、服务类型、签单销售、联系人、联系方式、举办酒店、举办日期、签单日期、收款性质、收款金额、收款日期
            $header = [
                'check_status' => '审核状态',
                'sign_company' => '签单公司',
                'cooperation' => '合作模式',
                'order_type' => '订单性质',
                'category' => '服务类型',
                'sales' => '签单销售',
                'bridegroom' => '新郎姓名',
                'bridegroom_mobile' => '新郎电话',
                'bride' => '新娘姓名',
                'bride_mobile' => '新娘电话',
                'event_date' => '举办日期',
                'sign_date' => '签约日期',
                'store' => '酒店名称',
                'income_type' => '收款性质',
                'income_total' => '收款金额',
                'income_date' => '收款日期',
            ];

            $rows = $model->with('order')->where($where)->order('id desc')->select();
            foreach ($rows as $row) {
                $row->append(['way', 'type', 'user', 'check_tag']);
                $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'order_type_text']);
                $expData[] = [
                    'check_status' => $row->check_tag,
                    'sign_company' => $row->order->company['title'],
                    'cooperation' => $row->order->cooperation['title'],
                    'order_type' => $row->order->order_type_text,
                    'category' => $row->order->category['title'],
                    'sales' => $row->order->user['realname'],
                    'bridegroom' => $row->order->bridegroom,
                    'bridegroom_mobile' => $row->order->bridegroom_mobile,
                    'bride' => $row->order->bride,
                    'bride_mobile' => $row->order->bride_mobile,
                    'event_date' => $row->order->event_date,
                    'sign_date' => $row->order->sign_date,
                    'store' => $row->order->store['title'],
                    'income_type' => $row->type['title'],
                    'income_total' => $row->total,
                    'income_date' => $row->income_date,
                ];
            }

            $fileName = '【收款单】' . date('YmdHis');
            $url = \app\common\third\Excel::exportExcel($expData, $header, $fileName);
            return success(['url' => $url], '导出成功');
        }
    }

    public function mine()
    {
        $post = $this->request->post();
        $user = $this->request->user;

        $where = [];
        $hasWhere = [];
        // 当前用户
        $hasWhere[] = ['user_id', '=', $user['id']];
        $hasWhere[] = ['refund_status', '=', 0];
        if (!empty($post['company_id'])) $hasWhere[] = ['company_id', 'in', $post['company_id']];
        if (!empty($post['store_id'])) $hasWhere[] = ['store_id', 'in', $post['store_id']];
        if (!empty($post['category_id'])) $hasWhere[] = ['category_id', 'in', $post['category_id']];
        if (!empty($post['order_type'])) $hasWhere[] = ['order_type', 'in', $post['order_type']];
        if (!empty($post['check_status'])) $hasWhere[] = ['check_status', 'in', $post['check_status']];
        if (!empty($post['sign_date_range'])) {
            $signDateRange = Format::dateRange($post['sign_date_range']);
            $hasWhere[] = ['sign_date', 'between', $signDateRange];
        }
        if (!empty($post['event_date_range'])) {
            $eventDateRange = Format::dateRange($post['event_date_range']);
            $hasWhere[] = ['event_date', 'between', $eventDateRange];
        }
        if (!empty($post['keyword'])) $hasWhere[] = ['contact_no|bridegroom|bridegroom_mobile|bride|bride_mobile', 'like', "%{$post['keyword']}%"];

        list($cfg) = Format::pagination($post);
        $model = OrderIncome::hasWhere('order', $hasWhere);
        $pg = $model->with('order')->where($where)->order('id desc')->paginate($cfg);
        $rows = $pg->getCollection();
        foreach ($rows as $row) {
            $row->append(['way', 'type', 'user', 'check_tag']);
            $row->order->append(['category', 'company', 'cooperation', 'user', 'store', 'hall', 'banquet_type', 'order_type_text', 'check_tag']);
        }

        $result = [];
        $result['list'] = $rows;
        $result['pagination'] = Format::pgResult($pg);

        return success($result);
    }
}
